<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript prompt() 方法差异解析</title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            color: #333;
            min-height: 100vh;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .container {
            max-width: 1200px;
            width: 100%;
            background: rgba(255, 255, 255, 0.95);
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            overflow: hidden;
            margin-top: 20px;
        }

        header {
            background: #4e54c8;
            color: white;
            padding: 25px;
            text-align: center;
        }

        h1 {
            font-size: 2.2rem;
            margin-bottom: 10px;
        }

        .subtitle {
            font-size: 1.1rem;
            opacity: 0.9;
        }

        .content {
            display: flex;
            flex-wrap: wrap;
            padding: 20px;
        }

        .example {
            flex: 1;
            min-width: 300px;
            padding: 20px;
            border-radius: 10px;
            margin: 10px;
            background: #f9f9f9;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .incorrect {
            border-top: 5px solid #ff4757;
        }

        .correct {
            border-top: 5px solid #2ed573;
        }

        h2 {
            margin-bottom: 15px;
            color: #2f3542;
            display: flex;
            align-items: center;
        }

        h2 i {
            margin-right: 10px;
        }

        .code-container {
            background: #2d3436;
            color: #f5f6fa;
            padding: 15px;
            border-radius: 8px;
            margin: 15px 0;
            font-family: 'Consolas', monospace;
            overflow-x: auto;
        }

        .output {
            background: #dfe4ea;
            padding: 15px;
            border-radius: 8px;
            margin: 15px 0;
            min-height: 100px;
        }

        .output h3 {
            margin-bottom: 10px;
            color: #2f3542;
        }

        .btn {
            background: #4e54c8;
            color: white;
            border: none;
            padding: 12px 20px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 1rem;
            transition: all 0.3s ease;
            margin-top: 10px;
        }

        .btn:hover {
            background: #3f44a5;
            transform: translateY(-2px);
        }

        .explanation {
            margin-top: 20px;
            padding: 20px;
            background: #f1f2f6;
            border-radius: 10px;
        }

        .visualization {
            display: flex;
            justify-content: space-around;
            margin: 20px 0;
            flex-wrap: wrap;
        }

        .step {
            text-align: center;
            margin: 10px;
            flex: 1;
            min-width: 200px;
        }

        .step-box {
            background: #4e54c8;
            color: white;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 10px;
            min-height: 80px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .arrow {
            font-size: 2rem;
            text-align: center;
            color: #4e54c8;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        @media (max-width: 768px) {
            .content {
                flex-direction: column;
            }

            .visualization {
                flex-direction: column;
            }

            .arrow {
                transform: rotate(90deg);
                margin: 10px 0;
            }
        }
    </style>
</head>

<body>
    <div class="container">
        <header>
            <h1>JavaScript prompt() 方法使用差异</h1>
            <p class="subtitle">理解两种写法为什么输出结果不同</p>
        </header>

        <div class="content">
            <div class="example incorrect">
                <h2><i class="fas fa-times-circle"></i> 不正确写法</h2>
                <div class="code-container">
                    <pre>prompt('请输入您的姓名');
let uname = prompt;
document.write(uname);</pre>
                </div>
                <div class="output">
                    <h3>输出结果：</h3>
                    <div id="output-incorrect"></div>
                </div>
                <button class="btn" onclick="runIncorrectExample()">运行示例</button>
            </div>

            <div class="example correct">
                <h2><i class="fas fa-check-circle"></i> 正确写法</h2>
                <div class="code-container">
                    <pre>let uname = prompt('请输入您的姓名');
document.write(uname);</pre>
                </div>
                <div class="output">
                    <h3>输出结果：</h3>
                    <div id="output-correct"></div>
                </div>
                <button class="btn" onclick="runCorrectExample()">运行示例</button>
            </div>
        </div>

        <div class="explanation">
            <h2>原理解析</h2>
            <p>在JavaScript中，<code>prompt</code>是一个函数，两种写法的区别在于：</p>

            <div class="visualization">
                <div class="step">
                    <div class="step-box">第一种写法<br>prompt('...')<br>然后 let uname = prompt</div>
                    <div class="arrow">↓</div>
                    <div class="step-box">显示提示框，但未保存返回值<br>然后将prompt函数本身赋值给uname</div>
                    <div class="arrow">↓</div>
                    <div class="step-box">输出的是prompt函数的代码</div>
                </div>

                <div class="step">
                    <div class="step-box">第二种写法<br>let uname = prompt('...')</div>
                    <div class="arrow">↓</div>
                    <div class="step-box">显示提示框并将用户输入的值赋给uname</div>
                    <div class="arrow">↓</div>
                    <div class="step-box">输出的是用户输入的字符串</div>
                </div>
            </div>

            <p><strong>关键区别：</strong>第一种写法中，<code>prompt('请输入您的姓名')</code>显示提示框但返回值没有被存储，随后<code>let uname = prompt</code>将函数本身（而不是返回值）赋值给变量。第二种写法正确地将函数返回值赋值给变量。
            </p>

            <div class="code-container">
                <pre>// 第一种情况（错误）：
prompt('请输入您的姓名'); // 显示提示框，但返回值未被使用
let uname = prompt;      // 将prompt函数本身赋值给uname
document.write(uname);   // 输出function prompt() { [native code] }

// 第二种情况（正确）：
let uname = prompt('请输入您的姓名'); // 显示提示框并将返回值赋值给uname
document.write(uname);               // 输出用户输入的字符串</pre>
            </div>
        </div>
    </div>

    <script>
        function runIncorrectExample() {
            // 保存原始document.write实现
            const originalWrite = document.write;

            // 重写document.write以捕获输出
            let outputContent = '';
            document.write = function (text) {
                outputContent += text;
            };

            // 运行不正确示例代码
            prompt('请输入您的姓名');
            let uname = prompt;
            document.write(uname);

            // 恢复原始document.write
            document.write = originalWrite;

            // 显示输出
            document.getElementById('output-incorrect').innerHTML =
                `<p>输出: <strong>${outputContent}</strong></p>
                 <p>这是因为将prompt函数本身赋值给了变量，而不是它的返回值。</p>`;
        }

        function runCorrectExample() {
            // 保存原始document.write实现
            const originalWrite = document.write;

            // 重写document.write以捕获输出
            let outputContent = '';
            document.write = function (text) {
                outputContent += text;
            };

            // 运行正确示例代码
            let uname = prompt('请输入您的姓名');
            document.write(uname);

            // 恢复原始document.write
            document.write = originalWrite;

            // 显示输出
            if (uname) {
                document.getElementById('output-correct').innerHTML =
                    `<p>输出: <strong>${outputContent}</strong></p>
                     <p>这是您输入的姓名，正确保存了prompt的返回值。</p>`;
            } else {
                document.getElementById('output-correct').innerHTML =
                    `<p>您取消了输入或未输入任何内容。</p>`;
            }
        }
    </script>

    <!-- Font Awesome for icons -->
    <script src="https://kit.fontawesome.com/a076d05399.js" crossorigin="anonymous"></script>
</body>

</html>